import pickle

from app.consts import CacheKey
from app.utils import get_cache_key


# 获取手机注册验证码 Redis缓存 Key
def get_phone_register_verify_code_key(phone):
    """
    获取手机注册验证码 Redis缓存 Key
    """
    return f"register|{phone}"


# 获取手机注册验证码
async def get_phone_register_verify_code(redis, phone):
    """
    获取手机注册验证码
    """
    # 获取缓存Key
    key = get_phone_register_verify_code_key(phone)
    # 设置Redis缓存
    return await redis.get(key)


# 设置手机注册验证码
async def set_phone_register_verify_code(redis, phone, code, expire=300):
    """
    设置手机注册验证码
    """
    # 获取缓存Key
    key = get_phone_register_verify_code_key(phone)
    # 设置Redis缓存
    return await redis.set(key, code, expire=expire)


# 获取手机号对应用户ID Redis缓存 Key
def get_phone_to_user_id_key(phone: str):
    """
    获取手机号对应用户ID Redis缓存 Key
    """
    return get_cache_key(key_list=[CacheKey.PhoneToUserID.value, phone])


# 设置手机号对应用户ID
async def set_phone_to_user_id(redis, phone, user_id, exist="", expire=0):
    # 获取缓存Key
    key = get_phone_to_user_id_key(phone)
    return await redis.set(key, user_id, exist=exist, expire=expire)


# 删除手机号对应用户ID
async def delete_phone_to_user_id(redis, phone):
    """
    设置手机号对应用户ID
    """
    # 获取缓存Key
    key = get_phone_to_user_id_key(phone)
    # 删除Redis缓存
    return await redis.delete(key)


# 获取手机号对应用户ID
async def get_phone_to_user_id(redis, phone):
    """
    获取手机号对应用户ID
    """
    # 获取缓存Key
    key = get_phone_to_user_id_key(phone)
    # 获取Redis缓存
    return await redis.get(key)